package com.wise.esexample.config;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * elasticsearch 配置类
 */
@Configuration
public class EsTransportConfig {

    /*@Value("${transport.elasticsearch.clusterNodes}")
    private String node;

    @Value("${transport.elasticsearch.clusterName}")
    private String clusterName;

    @Bean
    public TransportClient buildTransportClient(){
        System.out.println(node+"xxx"+clusterName);
        try {
            String[] params = node.split(":");
            Settings settings = Settings.builder()
                    .put("cluster.name", clusterName)
                    .build();
            TransportClient transportClient = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName(params[0]), Integer.parseInt(params[1])));
            return transportClient;
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return null;
        }
    }*/
    @Bean
    public TransportClient buildTransportClient() {
        try {
            Settings settings = Settings.builder()
                    .put("cluster.name", "lin-application")
                    .build();
            TransportClient transportClient = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.111"), 9300));
            return transportClient;
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return null;
        }
    }
}
